package replay;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import uk.ac.nott.cs.g53dia.Action;
import uk.ac.nott.cs.g53dia.CommunicateAction;
import uk.ac.nott.cs.g53dia.CreateNestAction;
import uk.ac.nott.cs.g53dia.DepositNectarAction;
import uk.ac.nott.cs.g53dia.FollowQueenAction;
import uk.ac.nott.cs.g53dia.HarvestAction;
import uk.ac.nott.cs.g53dia.LayEggAction;
import uk.ac.nott.cs.g53dia.MoveAction;
import uk.ac.nott.cs.g53dia.MoveTowardsAction;
import uk.ac.nott.cs.g53dia.Point;
import uk.ac.nott.cs.g53dia.RestAction;
import utils.Constants;
import utils.ControlPanel;
import utils.Simulation;

public class Player {

  private BufferedReader br;

  public Player(String replay) {
    try {
      br = new BufferedReader(new FileReader(replay));
      br.readLine();
      String[] params = br.readLine().split(";");
      ControlPanel.getInstance().setNumBees(params[0]);
      ControlPanel.getInstance().setDuration(params[1]);
      ControlPanel.getInstance().setRandomSeed(params[2]);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  /**
   * public static final int CREATE_NEST_ACTION_CODE = 0; public static final
   * int DEPOSIT_NECTAR_ACTION_CODE = 1; public static final int
   * HARVEST_ACTION_CODE = 2; public static final int LEY_EGG_ACTION_CODE = 3;
   * public static final int MOVE_ACTION_CODE = 4; public static final int
   * MOVE_TOWARDS_ACTION_CODE = 5; public static final int REST_ACTION_CODE = 6;
   * public static final int COMMUNICATE_ACTION_CODE = 7; public static final
   * int FOLLOW_QUEEN_ACTION_CODE = 8;
   */
  public Action readAction() {
    String line = null;
    while (true) {
      try {
        line = br.readLine();
        if (line != null && (line.startsWith("#") || "".equals(line))) {
          continue;
        } else if (line == null) {
          return null;
        } else break;
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    // line = line.trim();
    Action a = null;
    switch (Integer.parseInt(line.substring(0, 1))) {
    case Constants.CREATE_NEST_ACTION_CODE:
      a = new CreateNestAction();
      break;
    case Constants.DEPOSIT_NECTAR_ACTION_CODE:
      int amount = Integer.parseInt(line.split(Constants.ACTION_ARGS_SEPARATOR)[1]);
      a = new DepositNectarAction(amount);
      break;
    case Constants.HARVEST_ACTION_CODE:
      a = new HarvestAction();
      break;
    case Constants.LEY_EGG_ACTION_CODE:
      a = new LayEggAction();
      break;
    case Constants.MOVE_ACTION_CODE:
      int direction = Integer.parseInt(line.split(Constants.ACTION_ARGS_SEPARATOR)[1]);
      a = new MoveAction(direction);
      break;
    case Constants.MOVE_TOWARDS_ACTION_CODE:
      String point = line.split(Constants.ACTION_ARGS_SEPARATOR)[1];
      int x = Integer.parseInt(point.substring(1, point.indexOf(",")));
      int y = Integer.parseInt(point.substring(point.indexOf(",") + 1, point.length() - 1));
      Point target = new Point(x, y);
      a = new MoveTowardsAction(target);
      break;
    case Constants.REST_ACTION_CODE:
      a = new RestAction();
      break;
    case Constants.COMMUNICATE_ACTION_CODE:
      int type = Integer.parseInt(line.split(Constants.ACTION_ARGS_SEPARATOR)[1]);
      a = new CommunicateAction(type);
      break;
    case Constants.FOLLOW_QUEEN_ACTION_CODE:
      a = new FollowQueenAction();
      break;
    default:
      // error
      break;
    }
    return a;
  }

  public void close() {
    if (br != null) {
      try {
        br.close();
      } catch (IOException e) {
        e.printStackTrace();
      } finally {
        br = null;
      }
    }
  }

}
